﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

using TableTop.DataModel.Shelf;

namespace TableTop.Presentation.Controls
{
    [TemplatePart(Name = ContentGridPart, Type = typeof(Grid))]
    [TemplatePart(Name = CharacterMasterDataPart, Type = typeof(Grid))]
    [TemplatePart(Name = CharacterPhysicalPart, Type = typeof(Grid))]
    [TemplatePart(Name = CharacterAbilityPart, Type = typeof(Grid))]
    [TemplatePart(Name = SaveButtonPart, Type = typeof(Button))]
    public abstract class CharacterSheetEditor
        : ContentControl
    {
        protected const string ContentGridPart = "grdSheetContent";
        protected const string SaveButtonPart = "btnSaveButton";
        protected const string CharacterMasterDataPart = "CharacterMasterData";
        protected const string CharacterPhysicalPart = "CharacterPhysical";
        protected const string CharacterAbilityPart = "CharacterAbility";

        protected Grid grdSheetContent = null;
        protected Grid CharacterMasterData = null;
        protected Grid CharacterPhysical = null;
        protected Grid CharacterAbility = null;
        protected Button btnSaveButton = null;

        public event EventHandler<SaveCharacterSheetEventArgs> SaveSheet = null;

        public CharacterSheetEditor()
        {            
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.LocateControls();
            this.RegisterControlsEvents();
        }

        #region Virtual functions

        protected virtual void LocateControls()
        {
            this.btnSaveButton = this.Template.FindName(SaveButtonPart, this) as Button;
            this.grdSheetContent = this.Template.FindName(ContentGridPart, this) as Grid;
            this.CharacterMasterData = this.Template.FindName(CharacterMasterDataPart, this) as Grid;
            this.CharacterPhysical = this.Template.FindName(CharacterPhysicalPart, this) as Grid;
            this.CharacterAbility = this.Template.FindName(CharacterAbilityPart, this) as Grid;

            if (this.btnSaveButton == null)
                System.Diagnostics.Debug.WriteLine("Cannot locate " + SaveButtonPart);

            if (this.grdSheetContent == null)
                System.Diagnostics.Debug.WriteLine("Cannot locate " + ContentGridPart);

            if (this.CharacterMasterData == null)
                System.Diagnostics.Debug.WriteLine("Cannot locate " + CharacterMasterDataPart);

            if (this.CharacterAbility == null)
                System.Diagnostics.Debug.WriteLine("Cannot locate " + CharacterAbilityPart);

            if (this.CharacterPhysical == null)
                System.Diagnostics.Debug.WriteLine("Cannot locate " + CharacterPhysicalPart);
        }

        protected virtual void RegisterControlsEvents()
        {
            if (this.btnSaveButton != null)
                this.btnSaveButton.Click += new RoutedEventHandler(btnSaveButton_Click);
        }        

        protected virtual void OnSave()
        {            
        }

        protected void OnSaveBase(ICharacterSheetData Data)
        {
            SaveCharacterSheetEventArgs e = null;

            if (this.SaveSheet != null)
            {
                e = new SaveCharacterSheetEventArgs(Data);
                this.SaveSheet(this, e);
            }
        }

        #endregion

        #region Events

        protected void btnSaveButton_Click(object sender, RoutedEventArgs e)
        {
            this.OnSave();
        }

        #endregion
    }
}
